package core.gui;

import java.util.ArrayList;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import core.descriptors.TaskDescriptor;
import core.enums.TaskTYPE;
/**
 * 
 * @author Vincenzo Merlo, Scuderi Giovanni Luca, Paolo Giuffrida
 *
 */
public class ComboBoxTableModel extends AbstractTableModel {

	// Implementation of TableModel interface 

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();
	static String [] validStates  = new String[ TaskTYPE.values().length ];

	static {
		int i = 0;
		for (TaskTYPE task: TaskTYPE.values())
			validStates[i++] = task.name(); 
	}

	/**
	 *
	 * @return
	 */
	public int getRowCount() {
		return data.size();
	}

	/**
	 *
	 * @return
	 */
	public int getColumnCount() {
		return COLUMN_COUNT;
	}

	/**
	 *
	 * @param row
	 * @param column
	 * @return
	 */
	public Object getValueAt(int row, int column) {
		return data.get(row)[column];
	}



	/**
	 *
	 * @param column
	 * @return
	 */
	public String getColumnName(int column) {
		return columnNames[column];
	}


	/**
	 *
	 * @param value
	 * @param row
	 * @param column
	 */
	public void setValueAt(Object value, int row, int column) {
		data.get(row)[column] = value;
		TableModelEvent dogadjaj = new TableModelEvent(this, row,
				row, column, TableModelEvent.UPDATE);
		for (TableModelListener l: listeners)
			l.tableChanged(dogadjaj);
		fireTableRowsUpdated(row, row);
	}

	public void addRow(TaskDescriptor td)
	{
		data.add(new Object[] {
				td.getLink(),
				"Insert command here",
				td.getType()});

		TableModelEvent event = new TableModelEvent(this, 1,
				1, TableModelEvent.ALL_COLUMNS,
				TableModelEvent.INSERT);
		for (TableModelListener l: listeners)
			l.tableChanged(event);
	}

	@Override
	public void addTableModelListener(TableModelListener l)
	{
		if (listeners.contains(l))
			return;
		listeners.add(l);
	}

	/**
	 *
	 * @return
	 */
	 public static String[] getValidStates() {
		return validStates;
	}

	/**
	 *
	 * @param value
	 * @return
	 */
	 protected boolean isValidValue(Object value) {
		 if (value instanceof String) {
			 String sValue = (String)value;

			 for (int i = 0; i < validStates.length; i++) {
				 if (sValue.equals(validStates[i])) {
					 return true;
				 }
			 }
		 }

		 return false;
	 }

	 /**	
	  *
	  */
	 protected static final int COLUMN_COUNT = 3;

	 Class<?>[] types = new Class<?> [] {
			 java.lang.String.class, java.lang.String.class, java.lang.Object.class
	 };

	 boolean[] canEdit = new boolean [] {
			 false, true, true
	 };

	 /**
	  *
	  * @param columnIndex
	  * @return
	  */
	 public Class<?> getColumnClass(int columnIndex) {
		 return types [columnIndex];
	 }

	 /**
	  *
	  * @param rowIndex
	  * @param columnIndex
	  * @return
	  */
	 public boolean isCellEditable(int rowIndex, int columnIndex) {
		 return canEdit [columnIndex];
	 }

	 public void removeRow(int rowIndex) throws Exception {
		 data.remove(rowIndex);
		 TableModelEvent event = new TableModelEvent(this, rowIndex,
				 rowIndex, TableModelEvent.ALL_COLUMNS,
				 TableModelEvent.DELETE);
		 for (TableModelListener l: listeners)
			 l.tableChanged(event);
	 }

	 public void removeAll() {
		 data.clear();
		 TableModelEvent event = new TableModelEvent(this, 1,
				 1, TableModelEvent.ALL_COLUMNS,
				 TableModelEvent.DELETE);
		 for (TableModelListener l: listeners)
			 l.tableChanged(event);

	 }

	 /**
	  *
	  */
	  public ArrayList<Object[]> data = new ArrayList<Object[]>();


	  /**
	   *
	   */
	  protected static final String[] columnNames = {
		  "Link", "Command", "Type"
	  };

	  public ArrayList<TaskDescriptor> getData() {
		  ArrayList<TaskDescriptor> tds = new ArrayList<TaskDescriptor>();
		  for (int i = 0; i < getRowCount(); i ++ ) {
			  TaskDescriptor td = new TaskDescriptor();
			  td.setLink(getValueAt(i, 0).toString());
			  td.setCommand(getValueAt(i, 1).toString());
			  td.setType(TaskTYPE.valueOf(getValueAt(i, 2).toString()));
			  tds.add(td);
		  }

		  return tds;
	  } 


}
